#pragma once
#include<iostream>
#include<assert.h>
#include<string.h>

namespace me {


	class string
	{
	public:
		typedef char* iterator;
		typedef const char* const_iterator;

		//以前写法的构造函数
		string(const char* str = "")
			:_size(strlen(str))
		{
			_capacity = _size;
			_str = new char[_size + 1];
			memcpy(_str, str, _size + 1);
		}

		//现代写法的构造函数
		template<class InputIterator>
		string(InputIterator first, InputIterator last)
		{
			{
				while (first != last)
				{
					push_back(*first);
					++first;
				}
			}
		}

		string(const string& s);

		~string()
		{
			_str = nullptr;
			delete[] _str;
			_size = 0;
			_capacity = 0;

		}

		const char* c_str() const
		{
			return _str;
		}
		//迭代器
		iterator begin()
		{
			return _str;
		}

		iterator end()
		{
			return _str + _size;
		}

		const_iterator begin() const
		{
			return _str;
		}

		const_iterator end() const
		{
			return _str + _size;
		}


		size_t size() const;
		//返回下标
		char& operator[](size_t i);
		const char& operator[](size_t i) const;
		size_t capacity();

		void reserve(size_t n);

		void push_back(char ch);

		void append(const char* str);

		string& operator+=(char str);

		string& operator+=(const char* str);

		//string& operator=(const string& s);
		string& operator=(string tmp);

		void insert(size_t pos, char ch);
		void insert(size_t pos, const char* str);
		void erase(size_t pos, size_t len = npos);


		size_t find(char ch,size_t pos = 0);
		size_t find(const char* str, size_t pos = 0);

		string substr(size_t pos,size_t len = npos);

		void clear();

		void swap(string& s);

		bool operator<(const string& s);

		bool operator<=(const string& s);

		bool operator>(const string& s);

		bool operator>=(const string& s);

		bool operator==(const string& s);

		bool operator!=(const string& s);
	private:
		char* _str;
		size_t _size;
		size_t _capacity;

	public:
		const static size_t npos = -1;
	};
	
	std::ostream& operator<<(std::ostream& out, const string& s);

	std::istream& operator>>(std::istream& in, string& s);

}

